package com.system.dao.impl;


import com.system.config.Jdbc;
import com.system.dao.MainData;
import com.system.domin.MemoUser;
import com.system.untils.GetRandName;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.temporal.ValueRange;

public class MainDataMapper implements MainData {
    private final Jdbc jb;

    public MainDataMapper() {
        this.jb = new Jdbc();
    }


    @Override
    public void initMemoUser(MemoUser memoUser) {
        try {
            String sql = "insert into memouser values(?,?,?,?,?)";
            PreparedStatement preparedStatement = this.jb.prepareStatement(sql);
            preparedStatement.setString(1, memoUser.getEmail());
            preparedStatement.setString(2, memoUser.getName());
            preparedStatement.setInt(3,memoUser.getHistoricalList());
            preparedStatement.setInt(4,memoUser.getAllTopics());
            preparedStatement.setInt(5,memoUser.getUnread());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public MemoUser getInit(String email) {
        try {
            String sql = "select * from memouser where email='" + email + "'";
            ResultSet rs = jb.getStmt().executeQuery(sql);
            if (rs.next()) {
                return new MemoUser(
                        rs.getString("email"), rs.getString("name"),
                        rs.getInt("historicalList"), rs.getInt("allTopics"),rs.getInt("unread"));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    // 修改今日随机name
    @Override
    public void updateMainUser(String email) {
        try {
            GetRandName getRandName = new GetRandName();
//            MemoUser memoUser = getInit(email);
            String sql = "update memouser set name='" + getRandName.generateMeaningfulEnglishName() + "'where email='" + email + "'";
            jb.getStmt().executeUpdate(sql);
        } catch (SQLException e) {

            throw new RuntimeException(e);
        }
    }

    @Override
    public String getName(String email) {
        try {
            String sql = "select * from memouser where email='" + email + "'";
            ResultSet rs = jb.getStmt().executeQuery(sql);
            if (rs.next()) {
                return rs.getString("name");
            } else {
                System.out.println("没有找到该用户");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return "";
    }


    // 当发表一篇成功时，用户历史和all都有增加 对于已经注册的用户
    @Override
    public void updateAllTopic() {
        try {
            String sql = "update memouser set allTopics=allTopics+1";
            jb.getStmt().executeUpdate(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    // 用户隐藏一个帖子对应的我所有的allTopics -1
    @Override
    public void updateAllTopicDE() {
        try {
            String sql = "update memouser set allTopics=allTopics-1";
            jb.getStmt().executeUpdate(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    
    // 修改当前用户的历史
    @Override
    public void updateForUserHistory(String email) {
        try {
            String sql = "update memouser set historicalList=historicalList+1 where  email='" + email + "'";
            jb.getStmt().executeUpdate(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int selectHistoryAmount(String email) {
        try {
            String sql = "select historicalList from memouser where email='" + email + "'";
            ResultSet rs = jb.getStmt().executeQuery(sql);
            if (rs.next()) {
                return rs.getInt("historicalList");
            } else {
                return 0;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


}
